package com.karaoke.oksrv.service

import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.karaoke.oksrv.domain.DataDictionary
import com.karaoke.oksrv.domain.QSinger
import com.karaoke.oksrv.domain.Singer
import com.karaoke.oksrv.mapper.master.DataDictionaryMapper
import com.karaoke.oksrv.mapper.master.SingerMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.CacheConfig
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.util.StringUtils

/**
 * Created by allen on 2017/4/18.
 */
@Service
@CacheConfig(cacheNames = arrayOf("singers"))
@Transactional
class SingerService
{
    @Cacheable(key = "#p0.hashCode()")
    fun getSingers(params: QSinger): PageInfo<Singer>
    {
        PageHelper.startPage<Singer>(params.page, params.size).doSelectPageInfo<Singer> { }

        if (StringUtils.isEmpty(params.searchText))
        {
            return PageHelper.startPage<Singer>(params.page, params.size).doSelectPageInfo<Singer> {
                singerMapper.selectAll(params.typeId)
            }
        }
        else
        {
            return PageHelper.startPage<Singer>(params.page, params.size).doSelectPageInfo<Singer> {
                singerMapper.onSearch(params.searchText, params.typeId)
            }
        }
    }

    @Cacheable(key = "'SingerById_'+#p0")
    fun getSingerById(id: Int): Singer
    {
        return singerMapper.selectByPrimaryKey(id)
    }


    @Autowired lateinit var singerMapper: SingerMapper
    @Autowired lateinit var dataDictionaryMapper: DataDictionaryMapper
}